﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IIS根据9期更改版
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Socket serverSocker=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
            serverSocker.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"),11888));
            serverSocker.Listen(1000);
            label1.Text = "启动服务器";
            Thread cThread=new Thread(() =>
            {
                while (true)
                {
                    Socket clients = serverSocker.Accept();
                    byte[] bs = new byte[1024 * 1024 * 2];
                   int len= clients.Receive(bs,0,bs.Length,SocketFlags.None);
                    string requeststr = Encoding.UTF8.GetString(bs);
                   
                    HttpRequest httprequest=new HttpRequest(requeststr);
                    HttpContent content=new HttpContent(requeststr);
                    HttpApplication application=new HttpApplication();
                    application.ProcessRequest(content);
                    clients.Send(content.HttpResponse.GetResponseHeader());
                    clients.Send(content.HttpResponse.Body);
                    clients.Shutdown(SocketShutdown.Both);
                    clients.Close();
                }
            });
            cThread.IsBackground = true;
            cThread.Start();



        }
    }
}
